package com.edu.service.impl;

import com.edu.StatusCode;
import com.edu.dao.EduCurMapper;
import com.edu.dao.EduDeptMapper;
import com.edu.dao.EduTeacherMapper;
import com.edu.dao.EduTimeMapper;
import com.edu.pojo.EduCurriculum;
import com.edu.pojo.EduDept;
import com.edu.pojo.EduTime;
import com.edu.service.EduCurService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author YJF
 */
@Service
public class EduCurServiceImpl implements EduCurService {

    @Autowired
    private EduCurMapper eduCurMapper;

    @Autowired
    private EduTeacherMapper eduTeacherMapper;

    @Autowired
    private EduTimeMapper eduTimeMapper;

    @Autowired
    private EduDeptMapper eduDeptMapper;

    @Override
    public PageInfo<EduCurriculum> findByPage(String page, String size, String curName, List<Integer> allTecId) {
        PageHelper.startPage(Integer.parseInt(page), Integer.parseInt(size));
        List<EduCurriculum> list = eduCurMapper.findByPage(curName, allTecId);
        return new PageInfo<>(list);
    }

    @Override
    public List<EduTime> findTime() {
        return eduTimeMapper.selectAll();
    }

    @Override
    public int addCur(EduCurriculum eduCurriculum) {
        eduCurMapper.insertSelective(eduCurriculum);
        return StatusCode.OK;
    }

    @Override
    public EduCurriculum findOne(Long id) {
        // 根据ID搜寻课程信息
        EduCurriculum eduCurriculum = eduCurMapper.selectByPrimaryKey(id);
        // 时间信息
        eduCurriculum.setEduTime(eduTimeMapper.selectByPrimaryKey(eduCurriculum.getCurTime()));
        // 授课人信息
        eduCurriculum.setCurBy(eduTeacherMapper.selectByPrimaryKey(eduCurriculum.getTecId()).getTecName());
        // 班级信息
        eduCurriculum.setClsList(eduDeptMapper.findClsAndCur(id));
        return eduCurriculum;
    }

    @Override
    public int updateCur(EduCurriculum eduCurriculum) {
        eduCurMapper.updateByPrimaryKeySelective(eduCurriculum);
        return StatusCode.OK;
    }

    @Override
    public int delCur(Long id) {
        eduCurMapper.deleteByPrimaryKey(id);
        eduCurMapper.deleteCurCls(id);
        return StatusCode.OK;
    }

    @Override
    public int findCurAndCls(Integer id, Long curId) {
        int count = eduCurMapper.findCountCurAndCls(id, curId);
        if (count != 0){
            // 重复操作
            return StatusCode.REP_ERROR;
        }
        if (eduDeptMapper.selectByPrimaryKey(id) == null){
            return StatusCode.NOT_FIND;
        }
        return StatusCode.OK;
    }

    @Override
    public EduDept addCurAndCls(Integer id, Long curId) {
        eduCurMapper.addCurAndCls(id, curId);
        return eduDeptMapper.selectByPrimaryKey(id);
    }

    @Override
    public void delCurAndCls(Integer id, Long curId) {
        eduCurMapper.delCurAndCls(id, curId);
    }

    /**
     * 分院查询条件构建
     *
     * @param eduCurriculum 查询条件
     */
    public Example createExample(EduCurriculum eduCurriculum) {
        Example example = new Example(EduCurriculum.class);
        Example.Criteria criteria = example.createCriteria();
        if (eduCurriculum != null) {
            if (!StringUtils.isEmpty(eduCurriculum.getCurBy())) {
                criteria.andLike("curBy", "%" + eduCurriculum.getCurBy() + "%");
            }
            if (!StringUtils.isEmpty(eduCurriculum.getCurName())) {
                criteria.andLike("curName", "%" + eduCurriculum.getCurName() + "%");
            }
        }
        return example;
    }
}
